package cn.org.itool.ws.framework.config;

import cn.org.itool.ws.framework.ws.command.CommandHandlerMethodRegistry;
import cn.org.itool.ws.framework.ws.command.impl.DefaultCommandHandlerMethodRegistry;
import cn.org.itool.ws.framework.ws.group.WsChannelGroup;
import cn.org.itool.ws.framework.ws.group.WsDefaultChannelGroup;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 祥霸
 * @version 1.0.0
 */
@ConditionalOnClass(io.netty.bootstrap.ServerBootstrap.class)
@EnableConfigurationProperties(NettyProperties.class)
@Configuration(proxyBeanMethods = false)
public class WsConfigure {

    @Configuration(proxyBeanMethods = false)
    public static class CommonsConfigure {

        @Bean
        @ConditionalOnMissingBean(WsChannelGroup.class)
        public WsChannelGroup wsChannelGroup(){
            return new WsDefaultChannelGroup(GlobalEventExecutor.INSTANCE);
        }

        @Bean
        @ConditionalOnMissingBean(ObjectMapper.class)
        public ObjectMapper jacksonObjectMapper() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            objectMapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
            return objectMapper;
        }

        @Bean
        public CommandHandlerMethodRegistry commandHandlerMethodRegistry(){
            return new DefaultCommandHandlerMethodRegistry();
        }
    }
}
